<template>
  <div class="Role">
    <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>权限管理</el-breadcrumb-item>
  <el-breadcrumb-item>权限列表</el-breadcrumb-item>
</el-breadcrumb>
<el-button type="primary" plain @click="addre">添加用户</el-button>
<el-table
    class="mt-15"
    :data="RoleList"
    border
    style="width: 100%">
    <!-- 下拉选择开始 -->
    <el-table-column type="expand">
      <template slot-scope="scope">
        <el-row v-for='fsItme in scope.row.children' :key='fsItme.id'>
          <!-- 左边4份 -->
          <el-col :span='4'>
            <el-tag closable @close='handelRight(fsItme.id,scope.row)'
            v-show="scope.row.children.length !==0">{{fsItme.authName}}</el-tag>
             <i class="el-icon-arrow-right" v-if='fsItme.children.length >0'></i>
          </el-col>
          <!-- 右边20份里面又分为左边4份 右边20份 -->
          <el-col :span='20'>
            <el-row v-for='secItme in fsItme.children' :key='secItme.id'>
              <el-col :span='4' ><el-tag type='success' closable
              @close='handelRight(secItme.id,scope.row)'>{{secItme.authName}}</el-tag>
              <i class="el-icon-arrow-right" v-if='secItme.children.length >0'></i>
              </el-col>

              <el-col :span='20'>
                <el-tag type="warning" v-for='lastItme in secItme.children' :key='lastItme.id' closable @close='handelRight(lastItme.id,scope.row)'>{{lastItme.authName}}</el-tag>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-row v-show="scope.row.children.length ===0">
          <span>没有设置权限，请去设置</span>
        </el-row>
      </template>
      </el-table-column>
     <el-table-column
      type="index"
      width="50">
    </el-table-column>
    <el-table-column
      prop="roleName"
      label="角色名称"
      >
    </el-table-column>
    <el-table-column
      prop="roleDesc"
      label="描述"
      >
    </el-table-column>
    <el-table-column
      prop="level"
      label="操作"
      >
            <!-- 操作按钮部分开始 -->
      <template slot-scope='scope'>
        <!-- 点击把所有的信息传递过去 -->
      <el-button type="primary" icon="el-icon-edit" circle  @click="Edthand(scope.row)"></el-button>
      <el-button type="success" icon="el-icon-check" circle ></el-button>
      <el-button type="danger" icon="el-icon-delete" circle   @click='delhand(scope.row.id)'></el-button>
      </template>
    </el-table-column>

  </el-table>
  <!-- 添加弹框 -->
   <el-dialog title="添加角色" :visible.sync="aDdialogFormVisible">
      <el-form :model="aDform" :rules='aDformrules' ref='aDrefForm'>
        <el-form-item label="角色名称" :label-width="aDformLabelWidth" prop="username">
          <el-input v-model="aDform.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="aDformLabelWidth" prop="password">
          <el-input v-model="aDform.roleDesc"></el-input>
        </el-form-item>
      </el-form>
  <div slot="footer" class="dialog-footer">
  <!-- 点击取消弹框隐藏 -->
    <el-button @click="aDdialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addUser">确 定</el-button>
  </div>
  </el-dialog>

    <!-- 编辑 -->
   <el-dialog title="编辑角色" :visible.sync="EtdialogFormVisible">
      <el-form :model="Etform" ref='aDrefForm'>
        <el-form-item label="角色名称" :label-width="EtformLabelWidth" prop="username">
          <el-input v-model="Etform.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="EtformLabelWidth" prop="password">
          <el-input v-model="Etform.roleDesc"></el-input>
        </el-form-item>
      </el-form>
  <div slot="footer" class="dialog-footer">
  <!-- 点击取消弹框隐藏 -->
    <el-button @click="EtdialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="EtUser('aDrefForm')">确 定</el-button>
  </div>
  </el-dialog>
  </div>
</template>

<script>
import {getTubeList, getRoleDelList, RoleAddList, RoleDelList, RoleEdtList} from '@/api'
export default {
  data () {
    return {
      RoleList: [],
      aDdialogFormVisible: false,
      EtformLabelWidth: '80px',
      aDform: {
        roleName: '',
        roleDesc: ''
      },
      aDformrules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        admin2Desc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]

      },
      aDformLabelWidth: '80px',
      EtdialogFormVisible: false,
      Etform: {
        roleName: '',
        roleDesc: '',
        id: -1
      }
    }
  },
  created () {
    this.initTable()
  },
  methods: {
    initTable () {
      getTubeList()
        .then(res => {
          this.RoleList = res.data
        })
    },
    // 添加
    addre () {
      this.aDdialogFormVisible = true
    },
    // 添加完成
    addUser () {
      RoleAddList(this.aDform).then(res => {
        // console.log(res)
        this.initTable()
        this.aDdialogFormVisible = false
        this.aDform.roleName = ''
        this.aDform.roleDesc = ''
      })
    },
    handelRight (id, rid) {
      getRoleDelList(id, rid.id).then(res => {
        // console.log(res)
        // 之前的旧数据变成删除后服务器返回的新数据
        rid.children = res.data
      })
    },
    // 删除
    delhand (id) {
      // console.log(id)

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        RoleDelList(id)
          .then(res => {
            if (res.meta.status === 200) {
              this.initTable() // 重新刷新
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            } else {
              this.$message.error(res.res.meta.msg)
            }
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑
    Edthand (row) {
      this.EtdialogFormVisible = true
      this.Etform.id = row.id
      this.Etform.roleName = row.roleName
      this.Etform.roleDesc = row.roleDesc
    },
    EtUser (ref) {
      this.EtdialogFormVisible = false
      this.$refs[ref].validate((isPass) => {
        if (isPass) {
          // js校验成功，则发送请求
          RoleEdtList(this.Etform)
            .then(res => {
              // console.log(res)
              this.$message.success('编辑成功')
              this.EtdialogFormVisible = false
              this.initTable()
            })
        } else {
          this.$message.warning('请正确填写表单')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.Role{
  .el-tag{
    margin: 5px;
  }
}
</style>
